package gis.lifay.sdk;

import hdf.hdflib.HDFException;
import hdf.object.h4.H4SDS;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 *@ClassName ReadTest
 *@Description TODO
 *@Author lifay
 *@Date 2021/11/4 15:24
 **/
public class ReadTest {
    //private static String path = "C:\\Users\\lifay9092\\Documents\\swball\\hdf5\\yangli\\albedo_1km\\GLASS02B06.V04.A2000001.2017011.hdf";
    private static String path = "D:\\hdf5\\中文\\GLASS02B05.V04.A1981169.2018062.hdf";

    public static void main(String[] args) {
        HdfModel hdfModel = new HdfModel(path);
        try {
            hdfModel.open();
            Map<String, H4SDS> allSubDataSets = hdfModel.getAllSubDataSets();
            H4SDS h4SDS = allSubDataSets.get("ABD_BSA_VIS");
            short[] datas = (short[]) h4SDS.read();
            //short[][] shorts = StatisticsUtil.conversionArrays(datas, 3600, 7200);
            Map<String, Object> dataMetaData = hdfModel.getDataMetaData(h4SDS);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            hdfModel.close();
        }

    }

    private static void testcalculate(H4SDS h4SDS) throws HDFException {
        List metadata = h4SDS.getMetadata();
        short[] datas = (short[]) h4SDS.read();
        long calculate = calculate(datas, new short[]{0, 10000}, (short) -1, 0.0001);
        System.out.println(calculate);
    }

    private static Short[] test(short[] datas, Predicate<Short> test) {
        Short newdatas[] = new Short[datas.length];
        for (int i = 0; i < datas.length; i++) {
            newdatas[i] = datas[i];
        }
        return newdatas;

    }

    private static long calculate(short[] datas, short[] vaildRange, short fillValue, double scaleFactor) {
        float sum = 0;
        long vaildNum = 0;
        for (short data : datas) {
            if (fillValue == data || vaildRange[0] > data || data > vaildRange[1]) {
                continue;
            }
            //乘以比例系数
            double v = data * scaleFactor;
            sum += v;
            vaildNum++;
            System.out.println(v);
        }
        return (long) (sum / vaildNum);
    }
}